define([], function () {
    return String.prototype = ({
        isEmpty: function (str) {
            if (str == undefined || str == null || str.length == 0 /*|| str.trim() === ""*/) {
                return true;
            }
            return false;
        },
        //String.format("123[%s]",'c');
        format: function (str) {
            if (this.isEmpty(str)) {
                return "";
            }

            if (str.indexOf('%s') == -1) {
                return str;
            }

            var arr = str.split('%s');

            if (arr.length > arguments.length) {
                throw 'The number of actual parameters more than number of placeholders!';
            }

            var args = [];
            var str = arr[0];
            for (var arg in arguments) {
                if (arg == 0) {
                    continue;
                }
                str += arguments[arg] + arr[arg];
            }

            return str;
        },
        stringFormat: function (str) {
            if (this.isEmpty(str)) {
                return "";
            }

            if (str.indexOf('%s') == -1) {
                return str;
            }

            var arr = str.split('%s');

            if (arr.length > arguments.length) {
                throw 'The number of actual parameters more than number of placeholders!';
            }

            var args = [];
            var str = arr[0];
            for (var arg in arguments) {
                if (arg == 0) {
                    continue;
                }
                str += arguments[arg] + arr[arg];
            }

            return str;
        },

        isNotEmpty: function (str) {
            return !this.isEmpty(str);
        },

        //自动出现千分号
        comdify: function (n) {
            var re = /\d{1,3}(?=(\d{3})+$)/g;
            var n1 = n.replace(/^(\d+)((\.\d+)?)$/, function (s, s1, s2) {
                return s1.replace(re, "$&,") + s2;
            });
            return n1;
        },

        //移除千分位
        delPercentile: function (fromId) {
            $("#" + fromId + " input[data-type='number']").each(function () {
                var temp = $(this).val().replace(",", "");
                $(this).val(temp);
            });
        },

        //获取随机数
        random: function (n) {
            return Math.floor(Math.random() * n + 1)
        },

        //是否數字
        isNum: function (nubmer) {
            //判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/
            var re = /^[0-9]+.?[0-9]*$/;
            if (!re.test(nubmer)) {
                return false;
            }
            return true;
        },

        //获取当前时间
        getDateTime: function (format) {
            var defaultFormat = "%s-%s-%s %s:%s:%s";
            if (this.isNotEmpty(format)) {
                defaultFormat = format;
            }
            var date = new Date();
            return this.format(defaultFormat, date.getFullYear(), this.prevZerofill(date.getMonth(), 2), this.prevZerofill(date.getDate(), 2), this.prevZerofill(date.getHours(), 2), this.prevZerofill(date.getMinutes(), 2), this.prevZerofill(date.getSeconds(), 2));
        },
        //日期格式化
        formartDate:function(time,fmt){
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "H+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        },
        /**
         *
         * @param num
         * @param precision
         * @param separator
         * @returns {*}
         *=======================================================
         *     formatNumber(10000)="10,000"
         *     formatNumber(10000, 2)="10,000.00"
         *     formatNumber(10000.123456, 2)="10,000.12"
         *     formatNumber(10000.123456, 2, ' ')="10 000.12"
         *     formatNumber(.123456, 2, ' ')="0.12"
         *     formatNumber(56., 2, ' ')="56.00"
         *     formatNumber(56., 0, ' ')="56"
         *     formatNumber('56.')="56"
         *     formatNumber('56.a')=NaN
         *=======================================================
         */
        formatNumber: function (num, precision, separator) {
            var parts;
            // 判断是否为数字
            if (!isNaN(parseFloat(num)) && isFinite(num)) {
                // 把类似 .5, 5. 之类的数据转化成0.5, 5, 为数据精度处理做准, 至于为什么
                // 不在判断中直接写 if (!isNaN(num = parseFloat(num)) && isFinite(num))
                // 是因为parseFloat有一个奇怪的精度问题, 比如 parseFloat(12312312.1234567119)
                // 的值变成了 12312312.123456713
                num = Number(num);
                // 处理小数点位数
                num = (typeof precision !== 'undefined' ? num.toFixed(precision) : num).toString();
                // 分离数字的小数部分和整数部分
                parts = num.split('.');
                // 整数部分加[separator]分隔, 借用一个著名的正则表达式
                parts[0] = parts[0].toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + (separator || ','));

                return parts.join('.');
            }
            return NaN;
        },

        /**
         * 是否正整数
         * @returns {boolean}
         */
        isPositiveInteger: function () {
            var re = /^[0-9]+$/;
            return re.test(this);
        },

        //去除字符串左右两边空白
        trim: function () {
            return this.replace(/(^\s*)|(\s*$)/g, "");
        },

        //只去除字符串左边空白
        ltrim: function () {
            return this.replace(/(^\s*)/g, "");
        },

        //只去除字符串右边空白
        rtrim: function () {
            return this.replace(/(\s*$)/g, "");

        },

        //首字符大写
        capitalize: function (string) {
            var words = string.split(' ');
            for (var i = 0; i < words.length; i++) {
                words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
            }
            return words.join(' ');
        },

        //首字符小写
        deCapitalize: function (string) {
            var words = string.split(' ');
            for (var i = 0; i < words.length; i++) {
                words[i] = words[i].charAt(0).toLowerCase() + words[i].slice(1);
            }
            return words.join(' ');
        },
        //num传入的数字，n需要的字符长度
        prevZerofill: function (num, n) {
            return (Array(n).join(0) + num).slice(-n);
        },

        //String.format("123[{0},{1},{2}]",'a','b','c'); ==> 123[a,b,c]
        format: function (origin, args) {
            var result = origin;
            if (arguments.length > 0) {
                if (arguments.length == 1 && typeof (args) == "object") {
                    for (var key in args) {
                        if (args[key] != undefined) {
                            var reg = new RegExp("({" + key + "})", "g");
                            result = result.replace(reg, args[key]);
                        }
                    }
                }
                else {
                    for (var i = 0; i < arguments.length - 1; i++) {
                        if (arguments[i] != undefined) {
                            //var reg = new RegExp("({[" + i + "]})", "g");//这个在索引大于9时会有问题，谢谢何以笙箫的指出
                            var reg = new RegExp("({)" + i + "(})", "g");
                            var index = i + 1;
                            var str = arguments[index];
                            if (str == undefined) {
                                str = "";
                            }
                            result = result.replace(reg, str);
                        }
                    }
                }
            }
            return result;
        },
        hasEmptyCharactor: function (str) {
            if (this.isEmpty(str)) {
                return false;
            }

            var reg = /\W/g;
            var r = str.match(reg);
            if (r == null) {
                return false;
            }
            return true;
        },
        S4: function () {//用于生成uuid
            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        },
        GUID: function () {
            return (this.S4() + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + this.S4() + this.S4());
        }
    })
        ;
});